package com.app.twth.ui.cyclecount.sku;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;


import com.app.twth.databinding.ItemCycleCountSkuLpnListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.ui.cyclecount.CycleCountSku;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class CycleCountSkuLPNAdapter extends RecyclerView.Adapter<CycleCountSkuLPNAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;
    private List<CycleCountSku.InventoryListDTO> data = new ArrayList<>();

    public CycleCountSkuLPNAdapter(Context mContext, List<CycleCountSku.InventoryListDTO> data, MyOnClickListener myOnClickListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
    }

    public void setData(List<CycleCountSku.InventoryListDTO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemCycleCountSkuLpnListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemCycleCountSkuLpnListBinding itemBinding;

        public MyViewHolder(ItemCycleCountSkuLpnListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            CycleCountSku.InventoryListDTO inventoryDTO = data.get(position);
            itemBinding.tvItem.setText(inventoryDTO.getLocationCode() + "");
            if (Utils.isEmpty(inventoryDTO.getIbLpn())) {
                itemBinding.tvInventory.setText(inventoryDTO.getQty() + "(--)");
            } else {
                itemBinding.tvInventory.setText(inventoryDTO.getQty() + "(" + inventoryDTO.getIbLpn() + ")");
            }
            if (inventoryDTO.isCheck()) {
                itemBinding.ivFinish.setVisibility(View.VISIBLE);
            } else {
                itemBinding.ivFinish.setVisibility(View.INVISIBLE);
            }
            itemBinding.layout.setOnClickListener(v -> myOnClickListener.OnClick(position));
        }
    }
}
